<?php

namespace App\Admin\Actions\Grid;

use Dcat\Admin\Admin;
use Dcat\Admin\Grid\RowAction;

class ExtensionUninstall extends RowAction
{

    public function title()
    {
        $label = trans('admin.uninstall');

        return "<span class='text-danger'>{$label}</span>";
    }

    public function confirm()
    {
        return [trans('admin.confirm_uninstall'), $this->getKey()];
    }

    public function handle()
    {
        // 删除数据
        $manager = Admin::extension()
            ->updateManager()
            ->rollback($this->getKey());


        // 删除菜单
        Admin::extension()->get($this->getKey())->uninstall();

        // 删除代码
        __system__del__dir(admin_extension_path('/'.Admin::extension()->get($this->getKey())->getPackageName()));

        return $this
            ->response()
            ->success(implode('<br>', ($manager ? $manager->notes : ['卸载成功', $this->getKey()]) ))
            ->refresh();
    }
}
